# # 用random包做随机数
# import random
#
# with open("ips.txt", 'x') as fp:
#     for i in range(120000):
#         ip = "172.25.254." + str(random.randint(0, 255)) + '\n'
#         fp.write(ip)
#         print(ip)

# 用字典可以很好的统计字符串
ipn = {}
with open("ips.txt") as fp:
    for line in fp:
        if line in ipn:
            ipn[line] += 1
        else:
            ipn[line] = 1
    # 做降序排序 --》 sorted(可迭代对象,key=函数名,reverse=False/True)
    #                                                 True表示从大到小
    #                               key=lambda item: item[1]    返回？
    ipn = sorted(ipn.items(), key=lambda item: item[1], reverse=True)
    # 输出前10个
    count = 0
    for key, value in ipn:
        count += 1
        key = key[:-1]
        print(f"{key}   {value}")
        if count == 10:
            break
